import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView2.vue'
import { useCounterStore } from '@/stores/counter'



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  scrollBehavior(to,from,savedPosition) {
		if (savedPosition) {
			return savedPosition;
		} else {
			return { top: 0 };
		}
    
  },
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    },
    {
      path:'/login',
      name:'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path:'/register',
      name:'register',
      component: () => import('../views/RegisterView.vue')
    },
    {
      path:'/user',
      name:'user',
      component: () => import('../views/UserView.vue'),
      children:[
        {
          path:'order',
          component: () => import('../views/User/Order.vue')
        },
        {
          path:'trolley',
          component: () => import ('../views/User/Trolley.vue')
        },
        {
          path:'info',
          component: () => import('../views/User/UserInfo.vue')
        },
        {
          path:'safe',
          component: () => import('../views/User/Safe.vue')
        }
      ]
    },
    {
      path:'/bookInfo',
      name:'bookInfo',
      component: () => import('../views/BookInfo.vue')
    },
    {
      path:'/admin',
      name:'andmin',
      component: () => import('../views/AdminUser.vue'),
      children:[
        {
          path:'index',
          component: () => import('../views/Admin/index.vue')
        },
        {
          path:'us',
          component: () => import('../views/Admin/User.vue')
        },
        {
          path:'order',
          component: () => import('../views/Admin/Order.vue')
        },
        {
          path:'book',
          component: () => import('../views/Admin/Book.vue')
        },
      ]
    }
  ]
})



router.beforeEach((to,from) => {
  const store = useCounterStore ()
  // || to.path.includes('admin')
  if((to.path.includes('/user') || to.path == '/bookInfo' || to.path.includes('admin')) && !store.isLogin()){
    router.push('/login')
  }
})

export default router
